package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.entity.ReportTransfer;
import com.sc.nft.entity.ReportTransferDayDetails;
import com.sc.nft.entity.SecondOrder;
import com.sc.nft.entity.dto.ReportSaveDTO;
import com.sc.nft.entity.dto.ReportTransferTypeIncomeDTO;
import com.sc.nft.entity.vo.PageReportTransferVO;
import com.sc.nft.entity.vo.ReportTransferVO;
import com.sc.nft.enums.IncomeTypeEnum;
import com.sc.nft.enums.MarketTypeEnum;
import com.sc.nft.enums.StatisticalStatementEnum;
import com.sc.nft.helper.ReportOverviewHelper;
import com.sc.nft.service.ReportOverviewService;
import com.sc.nft.service.ReportTransferService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务接口实现
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-02-21 13:30:02
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ReportTransferServiceImpl implements ReportTransferService {

    private final ReportTransferDao reportTransferDao;

    private final OrderDao orderDao;

    private final SecondOrderDao secondOrderDao;

    private final FilterIncomeListDao filterIncomeListDao;

    private final UserIncomeRecordDao userIncomeRecordDao;

    private final ReportTransferDayDetailsDao reportTransferDayDetailsDao;

    private final ReportOverviewService reportOverviewService;

    @Override
    public void insertReportTransfer() {

        //交易数据统计总表(汇总)
        ReportTransfer reportTransfer = new ReportTransfer();
        reportTransfer.setSumStarterSellAmount(orderDao.getAllSumSellAmount());

        Boolean aBoolean = reportTransferDao.getByTime(DateUtil.format(DateTime.now(), "yyyy-MM-dd"));

        if (!aBoolean) {
            //市场账号
            List<Long> ids = filterIncomeListDao.getMarketAccountIds();
            reportTransfer.setSumBrandIncubationUserSellAmount(secondOrderDao.getSecondSellAmount(MarketTypeEnum.BRAND_INCUBATION, ids));
            reportTransfer.setSumBrandIncubationOtherSellAmount(secondOrderDao.getSecondSellAmountByMarketAccount(MarketTypeEnum.BRAND_INCUBATION, ids));
            reportTransfer.setSumFreeMarketUserSellAmount(secondOrderDao.getSecondSellAmount(MarketTypeEnum.FREE_MARKET, ids));
            reportTransfer.setSumFreeMarketOtherSellAmount(secondOrderDao.getSecondSellAmountByMarketAccount(MarketTypeEnum.FREE_MARKET, ids));
            //一级市场+品牌孵化+自由市场总销售额
            BigDecimal sumSellAmount = reportTransfer.getSumBrandIncubationUserSellAmount().add(reportTransfer.getSumFreeMarketUserSellAmount()).add(reportTransfer.getSumStarterSellAmount());
            reportTransfer.setSumSellAmount(sumSellAmount);
            reportTransfer.insert();
        }

        Boolean orderBoolean = reportTransferDayDetailsDao.getByTimeAndType(1, DateUtil.format(DateTime.now(), "yyyy-MM-dd"));
        //交易数据统计明细表(每日列表)
        //一级市场
        if (!orderBoolean) {
            ReportTransferDayDetails orderReportTransferDayDetails = new ReportTransferDayDetails();
            orderReportTransferDayDetails.setToDaySellCount(orderDao.getToDaySellCount());
            orderReportTransferDayDetails.setToDaySellAmount(orderDao.getToDaySellAmount());
            orderReportTransferDayDetails.setToDaySellBuyUserCount(orderDao.getToDaySellBuyUserCount());
            orderReportTransferDayDetails.setToDaySellUserCount(0);
            orderReportTransferDayDetails.setToDayPayBalanceAmount(BigDecimal.ZERO);
            orderReportTransferDayDetails.setToDaySumPushStraightIncome(userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.DIRECT_PURCHASE.getCode()));
            orderReportTransferDayDetails.setToDaySumPushBetweenIncome(userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.INTERIM_PURCHASE.getCode()));
            orderReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.NURTURING_AWARD_PURCHASE.getCode()));
            orderReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE.getCode()));
            orderReportTransferDayDetails.setToDaySumCityServerCentreIncome(userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.CITY_SERVER_SALES.getCode()));
            orderReportTransferDayDetails.setType(1);
            orderReportTransferDayDetails.insert();
        }

        Boolean brandIncubationBoolean = reportTransferDayDetailsDao.getByTimeAndType(2, DateUtil.format(DateTime.now(), "yyyy-MM-dd"));
        //品牌孵化二级数据
        ReportTransferDayDetails brandIncubationReportTransferDayDetails = new ReportTransferDayDetails();
        brandIncubationReportTransferDayDetails.setToDaySellCount(secondOrderDao.getToDaySellCountByType(MarketTypeEnum.BRAND_INCUBATION.getCode()));
        brandIncubationReportTransferDayDetails.setToDaySellAmount(secondOrderDao.getToDaySellAmount(MarketTypeEnum.BRAND_INCUBATION.getCode()));
        brandIncubationReportTransferDayDetails.setToDaySellBuyUserCount(secondOrderDao.getToDaySellBuyUserCount(MarketTypeEnum.BRAND_INCUBATION.getCode()));
        brandIncubationReportTransferDayDetails.setToDaySellUserCount(secondOrderDao.getToDaySellUserCount(MarketTypeEnum.BRAND_INCUBATION.getCode()));

        //BigDecimal toDayIncomePayBalanceNum = userWalletDetailDao.getToDayIncomePayBalanceNum();

        brandIncubationReportTransferDayDetails.setToDayPayBalanceAmount(secondOrderDao.getToDayBuyPayBalancePriceByType(MarketTypeEnum.BRAND_INCUBATION.getCode()));

        ReportTransferTypeIncomeDTO directPurchaseIncome = getIncome(IncomeTypeEnum.DIRECT_PURCHASE_FEE);
        ReportTransferTypeIncomeDTO interimPurchaseIncome = getIncome(IncomeTypeEnum.INTERIM_PURCHASE_FEE);
        brandIncubationReportTransferDayDetails.setToDaySumPushStraightIncome(directPurchaseIncome.getBrandIncubationIncome());
        brandIncubationReportTransferDayDetails.setToDaySumPushBetweenIncome(interimPurchaseIncome.getBrandIncubationIncome());

        BigDecimal toDaySumNurturingAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.NURTURING_AWARD_PURCHASE_FEE.getCode());
        brandIncubationReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(toDaySumNurturingAwardPurchaseIncome);

        BigDecimal toDaySumTeamContributionAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE_FEE.getCode());
        brandIncubationReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(toDaySumTeamContributionAwardPurchaseIncome);

        BigDecimal toDaySumCityServerCentreIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.CITY_SERVER_CENTRE.getCode());
        brandIncubationReportTransferDayDetails.setToDaySumCityServerCentreIncome(toDaySumCityServerCentreIncome);
        brandIncubationReportTransferDayDetails.setType(2);

        if (!brandIncubationBoolean) {
            brandIncubationReportTransferDayDetails.insert();
        }

        Boolean freeMarketBoolean = reportTransferDayDetailsDao.getByTimeAndType(3, DateUtil.format(DateTime.now(), "yyyy-MM-dd"));
        //自由市场二级数据
        ReportTransferDayDetails freeMarketReportTransferDayDetails = new ReportTransferDayDetails();
        freeMarketReportTransferDayDetails.setToDaySellCount(secondOrderDao.getToDaySellCountByType(MarketTypeEnum.FREE_MARKET.getCode()));
        freeMarketReportTransferDayDetails.setToDaySellAmount(secondOrderDao.getToDaySellAmount(MarketTypeEnum.FREE_MARKET.getCode()));
        freeMarketReportTransferDayDetails.setToDaySellBuyUserCount(secondOrderDao.getToDaySellBuyUserCount(MarketTypeEnum.FREE_MARKET.getCode()));
        freeMarketReportTransferDayDetails.setToDaySellUserCount(secondOrderDao.getToDaySellUserCount(MarketTypeEnum.FREE_MARKET.getCode()));
        freeMarketReportTransferDayDetails.setToDayPayBalanceAmount(secondOrderDao.getToDayBuyPayBalancePriceByType(MarketTypeEnum.FREE_MARKET.getCode()));
        freeMarketReportTransferDayDetails.setToDaySumPushStraightIncome(directPurchaseIncome.getFreeMarketIncome());
        freeMarketReportTransferDayDetails.setToDaySumPushBetweenIncome(interimPurchaseIncome.getFreeMarketIncome());
        freeMarketReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(toDaySumNurturingAwardPurchaseIncome);
        freeMarketReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(toDaySumTeamContributionAwardPurchaseIncome);
        freeMarketReportTransferDayDetails.setToDaySumCityServerCentreIncome(toDaySumCityServerCentreIncome);
        freeMarketReportTransferDayDetails.setType(3);

        if (!freeMarketBoolean) {
            freeMarketReportTransferDayDetails.insert();
        }
    }

    @Override
    public void insertReportTransferNewEdition() {

        List<ReportSaveDTO> list = new ArrayList<>();

        //交易数据统计总表(汇总)
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_PRIMARY_MARKET_SALES, Convert.toStr(orderDao.getAllSumSellAmount()));

        //市场账号
        List<Long> ids = filterIncomeListDao.getMarketAccountIds();

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_BRAND_INCUBATION_SALES_USER, secondOrderDao.getSecondSellAmount(MarketTypeEnum.BRAND_INCUBATION, ids));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.TOTAL_BRAND_INCUBATION_SALES_MARKET_VALUE, secondOrderDao.getSecondSellAmountByMarketAccount(MarketTypeEnum.BRAND_INCUBATION, ids));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_SALES_USERS, secondOrderDao.getSecondSellAmount(MarketTypeEnum.FREE_MARKET, ids));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_SALES_MARKET_VALUE, secondOrderDao.getSecondSellAmountByMarketAccount(MarketTypeEnum.FREE_MARKET, ids));
        List<ReportSaveDTO> collect = list.stream().filter(reportSaveDTO -> !reportSaveDTO.getType().equals(StatisticalStatementEnum.TOTAL_BRAND_INCUBATION_SALES_MARKET_VALUE.getCode()) && !reportSaveDTO.getType().equals(StatisticalStatementEnum.FREE_MARKET_SALES_MARKET_VALUE.getCode())).collect(Collectors.toList());
        // 把collect的list中的dataValue值转换为bigdecimal类型并且求和
        BigDecimal sumSellAmount = collect.stream().map(ReportSaveDTO::getDataValue).map(Convert::toBigDecimal).reduce(BigDecimal.ZERO, BigDecimal::add);
        //一级市场+品牌孵化+自由市场总销售额
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.MARKET_SALES, Convert.toStr(sumSellAmount));

        //交易数据统计明细表(每日列表)
        //一级市场
        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_NUMBER_OF_TRANSACTION_ORDERS_IN_THE_PRIMARY_MARKET, Convert.toStr(orderDao.getToDaySellCount()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_TRANSACTION_AMOUNT_IN_THE_PRIMARY_MARKET, orderDao.getToDaySellAmount());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_NUMBER_OF_TRANSACTION_BUYERS, Convert.toStr(orderDao.getToDaySellBuyUserCount()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_NUMBER_OF_TRANSACTION_SELLER_USERS, Convert.toStr(0));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_THE_AMOUNT_OF_MONEY_DEDUCTED_BY_RED_ENVELOPES, Convert.toStr(0));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_DIRECT_REVENUE_ON_THE_DAY, userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.DIRECT_PURCHASE.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_DAY_TO_DAY_EARNINGS, userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.INTERIM_PURCHASE.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_BREEDING_AWARD_OF_THE_DAY, userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.NURTURING_AWARD_PURCHASE.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_TEAM_CONTRIBUTION_AWARD_OF_THE_DAY, userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FIRST_ORDER_CITY_CONSIGNMENT_DIVIDEND_ON_THE_SAME_DAY, userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.CITY_SERVER_SALES.getCode()));

        // 当日培育奖
        BigDecimal toDaySumNurturingAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.NURTURING_AWARD_PURCHASE_FEE.getCode());
        // 当日团队贡献奖
        BigDecimal toDaySumTeamContributionAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE_FEE.getCode());
        // 当日城市寄售分红
        BigDecimal toDaySumCityServerCentreIncome = userIncomeRecordDao.getToDaySumIncomeByType(IncomeTypeEnum.CITY_SERVER_CENTRE.getCode());
        // 交易数量(交易订单数量)
        Integer toDaySellCountByType = secondOrderDao.getToDaySellCountByType(MarketTypeEnum.BRAND_INCUBATION.getCode());
        // 交易金额
        BigDecimal toDaySellAmount = secondOrderDao.getToDaySellAmount(MarketTypeEnum.BRAND_INCUBATION.getCode());
        // 交易买家用户数量
        Integer toDaySellBuyUserCount = secondOrderDao.getToDaySellBuyUserCount(MarketTypeEnum.BRAND_INCUBATION.getCode());
        // 交易卖家用户数量
        Integer toDaySellUserCount = secondOrderDao.getToDaySellUserCount(MarketTypeEnum.BRAND_INCUBATION.getCode());
        // 红包抵扣金额：单日付款时红包支付金额
        BigDecimal toDayBuyPayBalancePriceByType = secondOrderDao.getToDayBuyPayBalancePriceByType(MarketTypeEnum.BRAND_INCUBATION.getCode());
        // 当日直推收益
        ReportTransferTypeIncomeDTO directPurchaseIncome = getIncome(IncomeTypeEnum.DIRECT_PURCHASE_FEE);
        // 当日间推收益
        ReportTransferTypeIncomeDTO interimPurchaseIncome = getIncome(IncomeTypeEnum.INTERIM_PURCHASE_FEE);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_NUMBER_OF_TRANSACTION, Convert.toStr(toDaySellCountByType));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_TRANSACTION_AMOUNT, toDaySellAmount);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_NUMBER_OF_TRANSACTION_BUYERS, Convert.toStr(toDaySellBuyUserCount));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_NUMBER_OF_TRANSACTION_SELLER_USERS, Convert.toStr(toDaySellUserCount));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_THE_AMOUNT_OF_MONEY_DEDUCTED_BY_RED_ENVELOPES, toDayBuyPayBalancePriceByType);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_DIRECT_REVENUE_ON_THE_DAY, directPurchaseIncome.getBrandIncubationIncome());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_DAY_TO_DAY_EARNINGS, interimPurchaseIncome.getBrandIncubationIncome());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_BREEDING_AWARD_OF_THE_DAY, toDaySumNurturingAwardPurchaseIncome);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_TEAM_CONTRIBUTION_AWARD_OF_THE_DAY, toDaySumTeamContributionAwardPurchaseIncome);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.SECONDARY_MARKET_CITY_CONSIGNMENT_DIVIDEND_ON_THE_SAME_DAY, toDaySumCityServerCentreIncome);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_NUMBER_OF_TRANSACTION_ORDERS_IN_THE_PRIMARY_MARKET, Convert.toStr(secondOrderDao.getToDaySellCountByType(MarketTypeEnum.FREE_MARKET.getCode())));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_TRANSACTION_AMOUNT_IN_THE_PRIMARY_MARKET, secondOrderDao.getToDaySellAmount(MarketTypeEnum.FREE_MARKET.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_NUMBER_OF_TRANSACTION_BUYERS, Convert.toStr(secondOrderDao.getToDaySellBuyUserCount(MarketTypeEnum.FREE_MARKET.getCode())));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_NUMBER_OF_TRANSACTION_SELLER_USERS, Convert.toStr(secondOrderDao.getToDaySellUserCount(MarketTypeEnum.FREE_MARKET.getCode())));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_THE_AMOUNT_OF_MONEY_DEDUCTED_BY_RED_ENVELOPES, secondOrderDao.getToDayBuyPayBalancePriceByType(MarketTypeEnum.FREE_MARKET.getCode()));

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_DIRECT_REVENUE_ON_THE_DAY, directPurchaseIncome.getFreeMarketIncome());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_DAY_TO_DAY_EARNINGS, interimPurchaseIncome.getFreeMarketIncome());

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_BREEDING_AWARD_OF_THE_DAY, toDaySumNurturingAwardPurchaseIncome);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_TEAM_CONTRIBUTION_AWARD_OF_THE_DAY, toDaySumTeamContributionAwardPurchaseIncome);

        ReportOverviewHelper.generalReportingMethod(list, StatisticalStatementEnum.FREE_MARKET_CITY_CONSIGNMENT_DIVIDEND_ON_THE_SAME_DAY, toDaySumCityServerCentreIncome);
        if (CollUtil.isNotEmpty(list)) {
            reportOverviewService.saveBath(list);
        }
    }

//    @Override
//    public void insertReportTransferByDay(String day) throws ParseException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date createTime = sdf.parse(day);
//
//        //交易数据统计总表(汇总)
//        ReportTransfer reportTransfer = new ReportTransfer();
//        reportTransfer.setSumStarterSellAmount(orderDao.getAllSumSellAmountByDay(day));
//
//        //市场账号
//        List<Long> ids = filterIncomeListDao.getMarketAccountIds();
//        reportTransfer.setSumBrandIncubationUserSellAmount(secondOrderDao.getSecondSellAmountByDay(MarketTypeEnum.BRAND_INCUBATION, ids, day));
//        reportTransfer.setSumBrandIncubationOtherSellAmount(secondOrderDao.getSecondSellAmountByMarketAccountByDay(MarketTypeEnum.BRAND_INCUBATION, ids, day));
//        reportTransfer.setSumFreeMarketUserSellAmount(secondOrderDao.getSecondSellAmountByDay(MarketTypeEnum.FREE_MARKET, ids, day));
//        reportTransfer.setSumFreeMarketOtherSellAmount(secondOrderDao.getSecondSellAmountByMarketAccountByDay(MarketTypeEnum.FREE_MARKET, ids, day));
//        //一级市场+品牌孵化+自由市场总销售额
//        BigDecimal sumSellAmount = reportTransfer.getSumBrandIncubationUserSellAmount().add(reportTransfer.getSumFreeMarketUserSellAmount()).add(reportTransfer.getSumStarterSellAmount());
//        reportTransfer.setSumSellAmount(sumSellAmount);
//        reportTransfer.insert();
//
//        reportTransfer.setCreateTime(createTime);
//        reportTransfer.updateById();
//
//        //交易数据统计明细表(每日列表)
//        //一级市场
//        ReportTransferDayDetails orderReportTransferDayDetails = new ReportTransferDayDetails();
//        orderReportTransferDayDetails.setToDaySellCount(orderDao.getToDaySellCountByDay(day));
//        orderReportTransferDayDetails.setToDaySellAmount(orderDao.getToDaySellAmountByDay(day));
//        orderReportTransferDayDetails.setToDaySellBuyUserCount(orderDao.getToDaySellBuyUserCountByDay(day));
//        orderReportTransferDayDetails.setToDaySellUserCount(0);
//        orderReportTransferDayDetails.setToDayPayBalanceAmount(BigDecimal.ZERO);
//        orderReportTransferDayDetails.setToDaySumPushStraightIncome(userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.DIRECT_PURCHASE.getCode(), day));
//        orderReportTransferDayDetails.setToDaySumPushBetweenIncome(userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.INTERIM_PURCHASE.getCode(), day));
//        orderReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.NURTURING_AWARD_PURCHASE.getCode(), day));
//        orderReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE.getCode(), day));
//        orderReportTransferDayDetails.setToDaySumCityServerCentreIncome(userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.CITY_SERVER_SALES.getCode(), day));
//        orderReportTransferDayDetails.setType(1);
//        orderReportTransferDayDetails.insert();
//
//        orderReportTransferDayDetails.setCreateTime(createTime);
//        orderReportTransferDayDetails.updateById();
//
//        //品牌孵化二级数据
//        ReportTransferDayDetails brandIncubationReportTransferDayDetails = new ReportTransferDayDetails();
//        brandIncubationReportTransferDayDetails.setToDaySellCount(secondOrderDao.getToDaySellCountByTypeByDay(MarketTypeEnum.BRAND_INCUBATION.getCode(), day));
//        brandIncubationReportTransferDayDetails.setToDaySellAmount(secondOrderDao.getToDaySellAmountByDay(MarketTypeEnum.BRAND_INCUBATION.getCode(), day));
//        brandIncubationReportTransferDayDetails.setToDaySellBuyUserCount(secondOrderDao.getToDaySellBuyUserCountByDay(MarketTypeEnum.BRAND_INCUBATION.getCode(), day));
//        brandIncubationReportTransferDayDetails.setToDaySellUserCount(secondOrderDao.getToDaySellUserCountByDay(MarketTypeEnum.BRAND_INCUBATION.getCode(), day));
//
//        brandIncubationReportTransferDayDetails.setToDayPayBalanceAmount(secondOrderDao.getToDayBuyPayBalancePriceByTypeByDay(MarketTypeEnum.BRAND_INCUBATION.getCode(), day));
//
//        ReportTransferTypeIncomeDTO directPurchaseIncome = getIncomeByDay(IncomeTypeEnum.DIRECT_PURCHASE_FEE, day);
//        ReportTransferTypeIncomeDTO interimPurchaseIncome = getIncomeByDay(IncomeTypeEnum.INTERIM_PURCHASE_FEE, day);
//        brandIncubationReportTransferDayDetails.setToDaySumPushStraightIncome(directPurchaseIncome.getBrandIncubationIncome());
//        brandIncubationReportTransferDayDetails.setToDaySumPushBetweenIncome(interimPurchaseIncome.getBrandIncubationIncome());
//
//        BigDecimal toDaySumNurturingAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.NURTURING_AWARD_PURCHASE_FEE.getCode(), day);
//        brandIncubationReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(toDaySumNurturingAwardPurchaseIncome);
//
//        BigDecimal toDaySumTeamContributionAwardPurchaseIncome = userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.TEAM_CONTRIBUTION_AWARD_PURCHASE_FEE.getCode(), day);
//        brandIncubationReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(toDaySumTeamContributionAwardPurchaseIncome);
//
//        BigDecimal toDaySumCityServerCentreIncome = userIncomeRecordDao.getToDaySumIncomeByTypeByDay(IncomeTypeEnum.CITY_SERVER_CENTRE.getCode(), day);
//        brandIncubationReportTransferDayDetails.setToDaySumCityServerCentreIncome(toDaySumCityServerCentreIncome);
//        brandIncubationReportTransferDayDetails.setType(2);
//        brandIncubationReportTransferDayDetails.insert();
//        brandIncubationReportTransferDayDetails.setCreateTime(createTime);
//        brandIncubationReportTransferDayDetails.updateById();
//
//        //自由市场二级数据
//        ReportTransferDayDetails freeMarketReportTransferDayDetails = new ReportTransferDayDetails();
//        freeMarketReportTransferDayDetails.setToDaySellCount(secondOrderDao.getToDaySellCountByTypeByDay(MarketTypeEnum.FREE_MARKET.getCode(), day));
//        freeMarketReportTransferDayDetails.setToDaySellAmount(secondOrderDao.getToDaySellAmountByDay(MarketTypeEnum.FREE_MARKET.getCode(), day));
//        freeMarketReportTransferDayDetails.setToDaySellBuyUserCount(secondOrderDao.getToDaySellBuyUserCountByDay(MarketTypeEnum.FREE_MARKET.getCode(), day));
//        freeMarketReportTransferDayDetails.setToDaySellUserCount(secondOrderDao.getToDaySellUserCountByDay(MarketTypeEnum.FREE_MARKET.getCode(), day));
//        freeMarketReportTransferDayDetails.setToDayPayBalanceAmount(secondOrderDao.getToDayBuyPayBalancePriceByTypeByDay(MarketTypeEnum.FREE_MARKET.getCode(), day));
//        freeMarketReportTransferDayDetails.setToDaySumPushStraightIncome(directPurchaseIncome.getFreeMarketIncome());
//        freeMarketReportTransferDayDetails.setToDaySumPushBetweenIncome(interimPurchaseIncome.getFreeMarketIncome());
//        freeMarketReportTransferDayDetails.setToDaySumNurturingAwardPurchaseIncome(toDaySumNurturingAwardPurchaseIncome);
//        freeMarketReportTransferDayDetails.setToDaySumTeamContributionAwardPurchaseIncome(toDaySumTeamContributionAwardPurchaseIncome);
//        freeMarketReportTransferDayDetails.setToDaySumCityServerCentreIncome(toDaySumCityServerCentreIncome);
//        freeMarketReportTransferDayDetails.setType(3);
//        freeMarketReportTransferDayDetails.insert();
//
//        freeMarketReportTransferDayDetails.setCreateTime(createTime);
//        freeMarketReportTransferDayDetails.updateById();
//    }

    @Override
    public ReportTransferVO getReportTransfer() {
        return BeanUtil.copyProperties(reportTransferDao.getReportTransfer(), ReportTransferVO.class);
    }

    @Override
    public Page<PageReportTransferVO> getPageReportTransfer(Integer type, String beginTime, String endTime, Integer pageNo, Integer pageSize) {
        return reportTransferDao.getPageReportTransfer(new Page<>(pageNo, pageSize), type, beginTime, endTime);
    }

    //二级总直推收益 - 自由市场的总收益 =  品牌孵化的直推总收益
    //1.获取自由市场二级的订单
    //2.或者二级市场的某个类型的总收益
    //3.获取自由市场订单的某个总收益
    //2 -3 = 品牌孵化的某个类型的总收益
    private ReportTransferTypeIncomeDTO getIncome(IncomeTypeEnum incomeTypeEnum) {
        List<SecondOrder> orderList = secondOrderDao.getOrderListByMarketType(MarketTypeEnum.FREE_MARKET.getCode());
        List<String> orderNoList = orderList.stream().map(SecondOrder::getOrderNo).collect(Collectors.toList());

        //这个类型的昨日总收益
        BigDecimal sumIncome = userIncomeRecordDao.getToDaySumIncomeByType(incomeTypeEnum.getCode());
        //自由市场的 这个类型的昨日总收益
        BigDecimal freeMarketIncome = BigDecimal.ZERO;
        if (orderNoList.size() > 0) {
            freeMarketIncome = userIncomeRecordDao.getToDaySumIncomeByTypeAndOrderNo(incomeTypeEnum.getCode(), orderNoList);
        }

        //品牌孵化的 这个类型的昨日总收益
        BigDecimal brandIncubationIncome = sumIncome.subtract(freeMarketIncome);
        ReportTransferTypeIncomeDTO reportTransferTypeIncomeDTO = new ReportTransferTypeIncomeDTO();
        reportTransferTypeIncomeDTO.setFreeMarketIncome(freeMarketIncome);
        reportTransferTypeIncomeDTO.setBrandIncubationIncome(brandIncubationIncome);
        return reportTransferTypeIncomeDTO;
    }

//    private ReportTransferTypeIncomeDTO getIncomeByDay(IncomeTypeEnum incomeTypeEnum, String day) {
//        List<SecondOrder> orderList = secondOrderDao.getOrderListByMarketTypeByDay(MarketTypeEnum.FREE_MARKET.getCode(), day);
//        List<String> orderNoList = orderList.stream().map(SecondOrder::getOrderNo).collect(Collectors.toList());
//
//        //这个类型的昨日总收益
//        BigDecimal sumIncome = userIncomeRecordDao.getToDaySumIncomeByTypeByDay(incomeTypeEnum.getCode(), day);
//        //自由市场的 这个类型的昨日总收益
//        BigDecimal freeMarketIncome = BigDecimal.ZERO;
//        if (orderNoList.size() > 0) {
//            freeMarketIncome = userIncomeRecordDao.getToDaySumIncomeByTypeAndOrderNoByDay(incomeTypeEnum.getCode(), orderNoList, day);
//        }
//
//        //品牌孵化的 这个类型的昨日总收益
//        BigDecimal brandIncubationIncome = sumIncome.subtract(freeMarketIncome);
//        ReportTransferTypeIncomeDTO reportTransferTypeIncomeDTO = new ReportTransferTypeIncomeDTO();
//        reportTransferTypeIncomeDTO.setFreeMarketIncome(freeMarketIncome);
//        reportTransferTypeIncomeDTO.setBrandIncubationIncome(brandIncubationIncome);
//        return reportTransferTypeIncomeDTO;
//    }

}